package com.atguigu.annotation;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;


@Slf4j
public class AutoTest {
    public static void main(String[] args)  {

        CalcDemo calcDemo = new CalcDemo();
        int para1 = 1;
        int para2 = 0;

        AtomicInteger atomicInteger = new AtomicInteger();
        String filePath = "BugReport" + (DateUtil.format(new Date(), "yyyyMMddHHmmss"))+"txt";
        Method[] methods = calcDemo.getClass().getMethods();

        int count = 0;
        for (int i = 0; i < methods.length ; i++) {
            if(methods[i].isAnnotationPresent(AtguiguTest.class)){
                try {
                    methods[i].invoke(calcDemo,para1,para2);
                } catch (Exception e) {
                    count++;
                   log.info("异常名称:{},异常原因:{},",e.getCause().getClass().getSimpleName(), e.getCause().getMessage());
                    FileUtil.writeString(methods[i].getName()+"\t"+"出现了异常"+"\n",filePath, "utf-8");
                    FileUtil.appendString("异常名称:"+e.getCause().getClass().getSimpleName()+"\n",filePath, "utf-8");
                    FileUtil.appendString("异常原因:"+e.getCause().getMessage()+"\n",filePath, "utf-8");
                } finally {
                    FileUtil.appendString("异常数:"+count+"\n",filePath, "utf-8");
                    System.out.println("bug报告路径:" + "target/classes/"+filePath+".txt");
                }


            }

        }
    }
}
